package com.woniu.admin.backup;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.ResponseEntity;
import java.io.File;
import java.nio.file.*;

@RestController
public class VideoController {

    @PostMapping("/upload")
    public ResponseEntity<?> uploadVideo(@RequestParam("video") MultipartFile file) {
        try {
            String uploadDir = "uploads"; // 确保这个目录存在
            File uploadFolder = new File(uploadDir);
            if (!uploadFolder.exists()) {
                uploadFolder.mkdir(); // 创建目录
            }
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            Path path = Paths.get(uploadDir + File.separator + fileName);
            Files.copy(file.getInputStream(), path);
            return ResponseEntity.ok().body(new UploadResponse("/videos/" + fileName));
        } catch (Exception e) {
            e.printStackTrace(); // 打印堆栈跟踪
            return ResponseEntity.internalServerError().body("Error uploading video: " + e.getMessage());
        }
    }

    static class UploadResponse {
        private String videoUrl;

        public UploadResponse(String videoUrl) {
            this.videoUrl = videoUrl;
        }

        public String getVideoUrl() {
            return videoUrl;
        }

        public void setVideoUrl(String videoUrl) {
            this.videoUrl = videoUrl;
        }
    }
}